/*初始化数据表格*/
function initTable() {
    var url=getBasePath()+'/mdm/region/getListData.json';
    var tableId="table_data";
    $("#"+tableId).dataTable().fnDestroy();
    json = {};
    json.showLineNuber = false;
    json.id = tableId;
    json.pagesize=10;
    json.url = url ;
    json.param = [
        {"name": "id", "value": $.trim($("#id").val())},
        {"name": "regionCode", "value": $.trim($("#regionCode").val())},
        {"name": "regionName", "value": $.trim($("#regionName").val())},
        {"name": "parentId", "value": $.trim($("#parentId").val())},
        {"name": "status", "value": $.trim($("#status").val())},
        {"name": "regionType", "value": $.trim($("#regionType").val())},
        {"name": "pinyin", "value": $.trim($("#pinyin").val())},
        {"name": "quanpin", "value": $.trim($("#quanpin").val())},
        {"name": "latitude", "value": $.trim($("#latitude").val())},
        {"name": "longitude", "value": $.trim($("#longitude").val())},
        {"name": "remark", "value": $.trim($("#remark").val())},
    ];
    json.columns = [
        {
            "mData": "regionCode", "sWidth": "15%", "sClass": "text-center",
            "mRender": function (data, type, row) {
                if (data) {
					var detailStr="<a class='icondatatable' vb='详情' href=\"#\" onclick='toDetailPage(\""+data+"\");'><i class=\"icon-detail\"></i></a>";
                    var editStr="<a class='icondatatable' vb='修改' href=\"#\" onclick='toEditRegion(\""+data+"\");'><i class=\"icon-edit\"></i></a>";
                    var delStr="<a class='icondatatable' vb='删除' href=\"#\"  onclick='deleteRegion(\""+data+"\");'><i class=\"icon-bin\"></i></a>";

                    return detailStr+editStr+delStr;
                }
            }
        },
        {"mData": "id", "sClass": "text-center"},
        {"mData": "regionCode", "sClass": "text-center"},
        {"mData": "regionName", "sClass": "text-center"},
        {"mData": "parentId", "sClass": "text-center"},
        {"mData": "status", "sClass": "text-center"},
        {"mData": "regionType", "sClass": "text-center"},
        {"mData": "pinyin", "sClass": "text-center"},
        {"mData": "quanpin", "sClass": "text-center"},
        {"mData": "latitude", "sClass": "text-center"},
        {"mData": "longitude", "sClass": "text-center"},
        {"mData": "remark", "sClass": "text-center"},
    ];
    var oTable1 = TableManaged(json).init();
}
/*查询数据*/
function doQuery(){
    initTable();
}


/*重置查询条件*/
function resetQueryForm(){
	$("#id").val('');
	$("#regionCode").val('');
	$("#regionName").val('');
	$("#parentId").val('');
	$("#status").val('');
	$("#regionType").val('');
	$("#pinyin").val('');
	$("#quanpin").val('');
	$("#latitude").val('');
	$("#longitude").val('');
	$("#remark").val('');
}

/*删除数据*/
function deleteRegion(regionCode){
    var _url=getBasePath()+'/mdm/region/delete.json';
    $.Confirm('你确定要删除这条记录吗？','',function(){
        var region={};
        region.regionCode=regionCode;
        $.ajax({
            url:_url,
            type:'POST',
            dataType:'json',
            contentType:'application/json',
            data:JSON.stringify(region),
            success:function(data){
                if(data.status==200){
                    $.Success('删除成功',doQuery);
                }else{
                    var msg = data.message || "删除行政区域数据发生错误";
                    $.Error(msg,'',null);
                }
            }
        });
    });

}

/*跳转编辑数据页面*/
function toEditRegion(regionCode){
    var _url=getBasePath()+'/mdm/region/toEdit.htm?regionCode='+regionCode;
    location.href=_url;
}

/*编辑数据*/
function editRegion(){
    $validator =$("#region_edit_form").validator('isFormValid');
    if(!$validator){
        return;
    }
    var _url=getBasePath()+'/mdm/region/edit.json';
    var region={};
	region.id=$("#id").val();
	region.regionCode=$("#regionCode").val();
	region.regionName=$("#regionName").val();
	region.parentId=$("#parentId").val();
	region.status=$("#status").val();
	region.regionType=$("#regionType").val();
	region.pinyin=$("#pinyin").val();
	region.quanpin=$("#quanpin").val();
	region.latitude=$("#latitude").val();
	region.longitude=$("#longitude").val();
	region.remark=$("#remark").val();

    $.ajax({
        url:_url,
        type:'POST',
        dataType:'json',
        contentType:'application/json',
        data:JSON.stringify(region),
        success:function(data){
            if(data.status==200){
                $.Success('该记录已提交成功!',gotoListPage);
            }else{
                var msg = data.message || "编辑行政区域数据发生错误";
                $.Error(msg,'',null);
            }

        },
        error:function(data){
            JSON.stringify(data);
        }
    });
}

/* 跳转明细页面*/
function toDetailPage(code){
    var _url=getBasePath()+'/mdm/region/detail.htm?regionCode='+code;
    location.href=_url;
}
/*跳转新增数据页面*/
function toAddRegion(){
    var _url=getBasePath()+'/mdm/region/toAdd.htm';
    location.href=_url;
}

/*新增数据*/
function saveRegion(){
    $validator =$("#region_add_form").validator('isFormValid');
    if(!$validator){
        return;
    }
    var _url=getBasePath()+'/mdm/region/add.json';
    var region={};
	region.id=$("#id").val();
	region.regionCode=$("#regionCode").val();
	region.regionName=$("#regionName").val();
	region.parentId=$("#parentId").val();
	region.status=$("#status").val();
	region.regionType=$("#regionType").val();
	region.pinyin=$("#pinyin").val();
	region.quanpin=$("#quanpin").val();
	region.latitude=$("#latitude").val();
	region.longitude=$("#longitude").val();
	region.remark=$("#remark").val();
    
    $.ajax({
        url:_url,
        type:'POST',
        dataType:'json',
        contentType:'application/json',
        data:JSON.stringify(region),
        success:function(data){
            if(data.status==200){
                $.Success('该记录已提交成功!',gotoListPage);
            }else{
                var msg = data.message || "新增行政区域数据发生错误";
                $.Error(msg,'',null);
            }
        }
    });
}

/*跳转数据列表页面*/
function gotoListPage(){
    location.href=getBasePath()+'/mdm/region/list.htm';
}





